package com.wish.group05.system.service.impl;

import com.wish.group05.system.config.SmsProperties;
import com.wish.group05.system.service.SmsService;
import com.zhenzi.sms.ZhenziSmsClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class SmsServiceImpl implements SmsService {


    @Override
    public boolean sendShortMessage(String telephone,String code) {
        //整合榛子云短信服务
        String apiUrl = SmsProperties.API_URL;
        String appId = SmsProperties.APP_ID;
        String appSecret = SmsProperties.APP_SECRET;
        String templateId  = SmsProperties.TEMPLATE_ID;
        //然后创建一个map集合
        Map<String,Object> map = new HashMap<>();
        map.put("number",telephone);
        map.put("templateId",templateId);
        //然后创建一个字符串类型的数组用来存放模板参数
        String[] templateParams = new String[2];
        templateParams[0] = code+"";
        templateParams[1] = "2";
        map.put("templateParams",templateParams);
        ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
        try{
            String send = client.send(map);
            log.info("发送成功");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
